﻿using System.Design;
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Design;

namespace ClassLibrary {
    class MerdaEditor : UITypeEditor {
        private Type _designBindingPickerType;
        private Type _designBindingType;

        //private DesignBindingPicker designBindingPicker;
        private object designBindingPicker;
 
        public override bool IsDropDownResizable {
            get {
                return true;
            }
        }
 
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) {
            _designBindingPickerType = Type.GetType("System.Windows.Forms.Design.DesignBindingPicker, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
            _designBindingType = Type.GetType("System.Windows.Forms.Design.DesignBinding, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");

            if (provider != null && context.Instance != null) {
                PropertyDescriptor dataSourceProperty = TypeDescriptor.GetProperties(context.Instance)["DataSource"];
                if (dataSourceProperty != null) {
                    object dataSource = dataSourceProperty.GetValue(context.Instance);
                    if (designBindingPicker == null) {
                        //designBindingPicker = new DesignBindingPicker();
                        designBindingPicker = Activator.CreateInstance(_designBindingPickerType);
                    }

                    //DesignBinding oldSelection = new DesignBinding(dataSource, (string) value);
                    object oldSelection = Activator.CreateInstance(_designBindingType, new object[] { dataSource, (string) value} );

                    //DesignBinding newSelection = designBindingPicker.Pick(context, provider,
                    //                                                      false, /* showDataSources   */
                    //                                                      true,  /* showDataMembers   */
                    //                                                      true,  /* selectListMembers */
                    //                                                      dataSource, String.Empty,
                    //                                                      oldSelection);

                    object newSelection = designBindingPicker.GetType().GetMethod("Pick").Invoke(designBindingPicker, new object[] { 
                        context, provider,
                        false, /* showDataSources   */
                        true,  /* showDataMembers   */
                        true,  /* selectListMembers */
                        dataSource, String.Empty,
                        oldSelection
                    });

                    if (dataSource != null && newSelection != null) {
                        //value = newSelection.DataMember;
                        value = newSelection.GetType().GetProperty("DataMember").GetValue(newSelection, null);
                    }
                }
            }
 
            return value;
        }
 
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) {
            return UITypeEditorEditStyle.DropDown;
        }
    }
}
